#include "../source/server.hpp"

void OnConnected(const ConnectionPtr& conn)
{
    lg(Info,"NEW CONNECTION: %p, id:%d", conn.get(), conn->Id());
}

void OnMessage(const ConnectionPtr& conn, Buffer* buf)
{
    lg(Info,"server get message:%s", buf->ReadPosition());
    buf->MoveReaderOffset(buf->ReadAbleSize());
    std::string reply("Hello Client!");
    conn->Send(reply.c_str(), reply.size());
}

void CloseConntection(const ConnectionPtr& conn)
{
    lg(Info, "connection id:%d destroied.", conn->Id());
}


int main()
{
    TcpServer svr(8888);
    svr.SetThreadcount(2);
    svr.SetConnectedCallback(std::bind(&OnConnected, std::placeholders::_1));
    svr.SetMessageCallback(std::bind(&OnMessage, std::placeholders::_1, std::placeholders::_2));
    svr.SetCloseCallback(std::bind(&CloseConntection, std::placeholders::_1));
    svr.EnableInactiveRelease(10);
    svr.Start();
    return 0;
}